#!/bin/bash
trap 'echo Abort ; exit 1' ERR
err() { echo "Error. $@ not set."; exit 1; }
. varlist
[ -z "$SOURCE_DIR" ] && err SOURCE_DIR
[ -z "$BUILD_DIR" ] && err BUILD_DIR
[ -z "$STATIC_LIB_DIR" ] && err STATIC_LIB_DIR
[ -z "$GMP" ] && err GMP
[ -z "$MPFR" ] && err MPFR
# [ -z "$PATCH" ] && err PATCH
[ -z "$ZLIB" ] && err ZLIB
[ -z "$NCURSES" ] && err NCURSES
# [ -z "$M4" ] && err M4
[ -z "$LIBTOOL" ] && err LIBTOOL
[ -z "$FLEX" ] && err FLEX
[ -z "$BISON" ] && err BISON
# [ -z "$SED" ] && err SED
# [ -z "$GAWK" ] && err GAWK
[ -z "$TEXINFO" ] && err TEXINFO
[ -z "$GMP" ] && err GMP
[ -z "$MPFR" ] && err MPFR

echo Install static libs.
export PATH=$STATIC_LIB_DIR/bin/:$PATH
ARG=' --disable-shared --enable-static --without-shared --without-cxx'
export CFLAGS=$LIB_CFLAGS
export LDFLAGS="-L$STATIC_LIB_DIR/lib"

# zlib is special. they are compiled in the source folder
# zlib 's configure argument is special.
P=$ZLIB
if [ ! -f $STATIC_LIB_DIR/.$P ] ; then
	echo Build $P
	cd $SOURCE_DIR/$P
	./configure --prefix=$STATIC_LIB_DIR > /dev/null
	set -x
	make > /dev/null
	make install > /dev/null
	make distclean > /dev/null
	set +x
	touch $STATIC_LIB_DIR/.$P
fi

# THIS LINE IS PROLIXITY
# for P in $PATCH $NCURSES $M4 $FLEX $BISON $SED $GAWK $GETTEXT $TEXINFO $GMP $MPFR #$LIBTOOL 
for P in $NCURSES $FLEX $BISON $GETTEXT $GMP $MPFR 
do

if [ ! -f $STATIC_LIB_DIR/.$P ] ; then
	echo Build $P
	rm $BUILD_DIR/$P -rf
	mkdir $BUILD_DIR/$P
	cd $BUILD_DIR/$P
	$SOURCE_DIR/$P/configure --prefix=$STATIC_LIB_DIR --disable-shared --enable-static --without-shared --without-cxx --with-gmp=$STATIC_LIB_DIR > /dev/null
	set -x
	make > /dev/null
	make install > /dev/null
	set +x
	touch $STATIC_LIB_DIR/.$P
fi

done
